var requestSign = require('./utils/requestData.js');
App({
  /**
  *当小程序初始化完成时，会触发 onLaunch（全局只触发一次）
  */
  onLaunch: function () {
    const that = this;
    that.mpUpdate();
    //获取第三个自定义域名地址
    const extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {}
    wx.setStorageSync("publicUrl", extConfig.domain);
    that.publicUrl = extConfig.domain
    
    this.globalData = {
      tab_list: extConfig.pathList,
      config: '',
      //搜索标识
      searchSign:'',
      //搜索店铺关键字
      searchShopKey:'',
      //分销的文案数据
      distributionData:'',
      //分红文案数据
      bonusData:'',
      //当账户体系为3，绑定手机为0时，手机将不再需要验证
      no_check_phone:'',
      bonusData:'',
      is_password_set:'',
      //授权证书-证书类型 1-分销中心 2-分红中心 3-微商中心 4-微店
      credential_type:'',
    }

   
  },

  /**
  *当小程序启动，或从后台进入前台显示，会触发 onShow
  */
  onShow: function (options) {
    const that = this;    
    let user_token = wx.getStorageSync('user_token');    
    if (user_token != ''){
      this.getMember();
    }
    that.configFun();    
  },

  keyword: '02cff04ecdcf64bb',
  publicUrl: '',
  userToken: '',
  userTokenEvent: '',
  loginStatus: false,//登录状态，true-已登录，false-未登录
  header: {
    'Content-Type': 'application/json; charset=utf-8',
    'X-Requested-With': 'XMLHttpRequest',
    'Program':'miniProgram',        
    'user-token': wx.getStorageSync('user_token'),
    'Cookie': wx.getStorageSync('setCookie'), 
    "website-id": wx.getExtConfigSync().website_id,   
  },
  
  

  // onHide:function(){
  //   let pages_list = getCurrentPages();
  //   console.log(pages_list);
  //   for(let item of pages_list){
  //     if (item.route == 'pages/goods/detail/index'){
  //       wx.switchTab({
  //         url: '/pages/index/index',
  //       })
  //     }
  //   }      
  // },

  // onHide:function(){
  //   wx.switchTab({
  //     url: '/pages/index/index',
  //   })    
  // },

  
  getMember: function () {
    const that = this;
    const extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {}
    let publicUrl = extConfig.domain
    var postData = {};
    let datainfo = requestSign.requestSign(postData);
    let header = {
      'Content-Type': 'application/json; charset=utf-8',
      'X-Requested-With': 'XMLHttpRequest',
      'Program': 'miniProgram',
      'user-token': wx.getStorageSync('user_token'),
      'Cookie': wx.getStorageSync('setCookie'),
      "website-id": wx.getExtConfigSync().website_id,
    }
    header.sign = datainfo;
    wx.request({
      url: publicUrl + '/wapapi/Member/memberIndex',
      data: postData,
      header: header,
      method: 'POST',
      dataType: 'json',
      responseType: 'text',
      success: (res) => {
        if(res.data.code == -1000){
          wx.redirectTo({
            url: '/pages/logon/index',
          })
        }else{
          that.globalData.is_password_set = res.data.data.is_password_set;
        }

      },
      fail: (res) => { },
    })
  },

  //系统配置
  configFun: function () {
    const that = this;
    const extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {}
    let publicUrl = extConfig.domain
    var postData = {};
    let datainfo = requestSign.requestSign(postData);
    let header = {
      'Content-Type': 'application/json; charset=utf-8',
      'X-Requested-With': 'XMLHttpRequest',
      'Program': 'miniProgram',
      'user-token': wx.getStorageSync('user_token'),
      'Cookie': wx.getStorageSync('setCookie'),
      "website-id": wx.getExtConfigSync().website_id,
    }
    header.sign = datainfo;
    wx.request({
      url: publicUrl + '/wapapi/config',
      data: postData,
      header: header,
      method: 'POST',
      dataType: 'json',
      responseType: 'text',
      success: (res) => {
        if(res.data.code == 1){
          that.globalData.config = res.data.data
          if (res.data.data.config.account_type == 3 && res.data.data.config.is_bind_phone == 0){
            that.globalData.no_check_phone = 0
          }
        }

        
      },
      fail: (res) => { },
    })

  },


  

  //小程序更新
  mpUpdate:function(){
    if (wx.canIUse('getUpdateManager')) {
      const updateManager = wx.getUpdateManager()
      updateManager.onCheckForUpdate(function (res) {
        // 请求完新版本信息的回调
        if (res.hasUpdate) {
          updateManager.onUpdateReady(function () {
            wx.showModal({
              title: '更新提示',
              content: '新版本已经准备好，是否重启应用？',
              success: function (res) {
                if (res.confirm) {
                  wx.clearStorageSync()
                  // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
                  updateManager.applyUpdate()
                }
              }
            })
          })
          updateManager.onUpdateFailed(function () {
            // 新的版本下载失败
            wx.showModal({
              title: '已经有新版本了哟~',
              content: '新版本已经上线啦~，请您删除当前小程序，重新搜索打开哟~',
            })
          })
        }
      })
    } else {
      // 如果希望用户在最新版本的客户端上体验您的小程序，可以这样子提示
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
      })
    }
  },


  
  





  
})
